package com.example.test.mapper;

import com.example.test.entity.Role;
import com.example.test.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Select("select * from user;")
    List<User> findAll();

    @Insert("insert into user value (#{id},#{username},#{password},#{role_id});")
    void insert(User user);

    @Delete("delete from user where id=#{id};")
    void delete(Integer id);

    @Update("update user set username = #{username} where id=#{id} ;")
    void update(User user);

    @Select("select * from user ;")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "role_id",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.example.test.mapper.RoleMapper.findByRoleId")
            )
    })
    List<User> findAllWithRole();

    @Select("select * from user ;")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "role_id",
                    property = "repos",
                    javaType = List.class,
                    many = @Many(select = "com.example.test.mapper.ReposMapper.findByRoleId")
            )
    })
    List<User> findRepos();
}
